package com.ybkj.o2o.sales.common;

import java.util.Random;

public class RandomString {
  private static Random randGen = null;
  private static char[] numbersAndLetters = null;

  /**
   * 取长度.
   */
  public static final String randomString(int length) {
    if (length < 1) {
      return null;
    }
    if (randGen == null) {
      randGen = new Random();
      numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz").toCharArray();
      // numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
    }
    char[] randBuffer = new char[length];
    for (int i = 0; i < randBuffer.length; i++) {
      // randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
      randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
    }
    return new String(randBuffer);
  }

  /**
   * 取出一个指定长度大小的随机正整数.
   * 
   * @param length int 设定所取出随机数的长度。length小于11
   * @return int 返回生成的随机数。
   */
  public static int randomNum(int length) {
    int num = 1;
    double random = Math.random();
    if (random < 0.1) {
      random = random + 0.1;
    }
    for (int i = 0; i < length; i++) {
      num = num * 10;
    }
    return (int) ((random * num));
  }
}
